//
//  AppDelegate.m
//  SSSMerchant
//
//  Created by xnxin on 2017/3/18.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "AppDelegate.h"
#import <QMUIKit/QMUIKit.h>
#import "XHKitMacro.h"
#import "SSSUIConfigurationTemplate.h"
#import "UIColor+Hexadecimal.h"
#import "SSSPersonCenterController.h"
#import "SSSMyIncomeViewController.h"
#import "SSSHomeViewController.h"
#import "SSSLoginViewController.h"
#import "SSSGuideView.h"
#import "RootViewController.h"
#import "SSSBaseNavigationViewController.h"
#import "SSSGuiedViewController.h"

#define kApplicationHasShowIntroViewKey @"ApplicationHasShowIntroViewKey"
#define kApplicationVersion             @"ApplicationVersion"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    
    // 启动QMUI的配置模板
    [SSSUIConfigurationTemplate setupConfigurationTemplate];
    
    // 将全局的控件样式渲染出来
    [QMUIConfigurationManager renderGlobalAppearances];

    self.window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT)];
    self.window.tintColor = [UIColor colorWithHex:@"40B87B"];
    
    //self.window.rootViewController = [[RootViewController alloc]init];
    
    [self setRootViewController];
    
//    [self login];
   // [self logout];
//    [SSSShowGuideView show];
    [self.window makeKeyAndVisible];
    NSLog(@"sandbox:\n%@",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]);
    return YES;
}

- (void)setRootViewController{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //💖 获取当前版本
    NSString *curVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    //💖 获取之前存储版本
    NSString *lastVersion = [userDefault objectForKey:kApplicationVersion];
    //💖 有新版本, 需要显示引导页
    if (![curVersion isEqualToString:lastVersion])
    {
        [userDefault setBool:NO
                      forKey:kApplicationHasShowIntroViewKey];
        [userDefault setObject:[NSBundle mainBundle].infoDictionary[@"CFBundleVersion"]
                        forKey:kApplicationVersion];
        // 加载引导页
        //[self addGuidePageView];
        self.window.rootViewController = [[SSSGuiedViewController alloc]init];
        
    }else{
        
        self.window.rootViewController = [[SSSLoginViewController alloc]init];
        
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (void)login{
   
    SSSHomeViewController *home=[SSSHomeViewController new];
    QMUINavigationController *homeNav= [[QMUINavigationController alloc]
                                        initWithRootViewController:home];
    homeNav.tabBarItem.title = @"首页";
    homeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_06"];

    UIViewController *inCome = [SSSMyIncomeViewController new];
    QMUINavigationController *inComeNav = [[QMUINavigationController alloc]
                                           initWithRootViewController:inCome];
    inComeNav.tabBarItem.title = @"我的收益";
    inComeNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_07"];

    SSSPersonCenterController *personal = [SSSPersonCenterController new];
    QMUINavigationController *personalNav = [[QMUINavigationController alloc]
                                             initWithRootViewController:personal];
    personalNav.tabBarItem.title = @"个人中心";
    personalNav.tabBarItem.image = [UIImage imageNamed:@"icon_home_08"];

    QMUITabBarViewController *tabVc = [QMUITabBarViewController new];
    tabVc.viewControllers = @[homeNav, inComeNav, personalNav];

    self.window.rootViewController = tabVc;
}

- (void)logout{
    
    SSSLoginViewController *login = [SSSLoginViewController new];
    QMUINavigationController *loginNav= [[QMUINavigationController alloc]
                                         initWithRootViewController:login];
    
    self.window.rootViewController = loginNav;
}


@end
